package com.shinhye.elephantsafe;

import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.shinhye.elephantsafe.ui.SetStatusRelativeLayout;

public class GuardSetup2Activity extends GuardSetupBaseActivity {

	private SetStatusRelativeLayout ssrl;
	private TelephonyManager tm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide2);
		
		ssrl = (SetStatusRelativeLayout) findViewById(R.id.ssrl_simbind);
		tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		
		String sim = getSP().getString("SIM", null);
		if(TextUtils.isEmpty(sim)) {
			ssrl.setChecked(false);
		} else {
			ssrl.setChecked(true);
		}
		
		ssrl.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Editor editor = getSP().edit();
				if(ssrl.isChecked()) {
					editor.putString("SIM", null);
					ssrl.setChecked(false);
				} else {
					editor.putString("SIM", tm.getSimSerialNumber());
					ssrl.setChecked(true);
				}
				
				editor.commit();
			}
		});
	}

	@Override
	public void showNext() {
		String sim = getSP().getString("SIM", null);
		if(TextUtils.isEmpty(sim)){
			Toast.makeText(this, "请绑定SIM卡以执行接下来的操作", Toast.LENGTH_SHORT).show();
			return;
		}
		Intent intent = new Intent(this, GuardSetup3Activity.class);
		startActivity(intent);
		finish();
		overridePendingTransition(R.anim.translate_next_in, R.anim.translate_next_out);
	}

	@Override
	public void showPrev() {
		Intent intent = new Intent(this, GuardSetup1Activity.class);
		startActivity(intent);
		finish();
		overridePendingTransition(R.anim.translate_prev_in, R.anim.translate_prev_out);
	}
}
